import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Solution3 {
    public int[][] highestPeak(int[][] isWater) {
        int m = isWater.length;
        int n = isWater[0].length;
        int[] dx = {0, 0, 1, -1};
        int[] dy = {1, -1, 0, 0};
        Queue<int[]> q = new LinkedList<>();
        int[][] hight = new int[m][n];

        for(int i = 0; i < m; i++) {
            Arrays.fill(hight[i], -1);
        }

        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(isWater[i][j] == 1) {
                    hight[i][j] = 0;
                    q.add(new int[]{i, j});
                }
            }
        }

        while(!q.isEmpty()) {
            int[] t = q.poll();
            int a = t[0];
            int b = t[1];
            for(int k = 0; k < 4; k++) {
                int x = a + dx[k];
                int y = b + dy[k];
                if(x >= 0 && x < m && y >= 0 && y < n && hight[x][y] == -1) {
                    hight[x][y] = hight[a][b] + 1;
                    q.add(new int[]{x, y});
                }
            }
        }
        return hight;
    }
}
